package com.atguigu.gmall.product.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.BaseCategoryViewService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.atguigu.gmall.product.service.SkuManageService;
import com.atguigu.gmall.product.service.SpuManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-06-10 10:14
 */
@Slf4j
@RestController
@RequestMapping("/api/product")
public class ProductApiController {

    @Autowired
    private SkuManageService skuManageService;

    @Autowired
    private SpuManageService spuManageService;

    @Autowired
    private BaseCategoryViewService baseCategoryViewService;

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 根据SkuID查询商品信息-包含图片列表
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId, @RequestHeader(required = false, value = "testheader") String header) {
        log.info("收到头信息:" + header);
        return skuManageService.getSkuInfoFromDB(skuId);
    }


    /**
     * 根据三级分类ID获取分类信息
     *
     * @param category3Id
     * @return
     */
    @GetMapping("/inner/getCategoryView/{category3Id}")
    public BaseCategoryView getCategoryView(@PathVariable("category3Id") Long category3Id) {
        return skuManageService.getCategoryView(category3Id);
    }


    /**
     * 根据SKuID查询商品最新价格
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable("skuId") Long skuId) {
        return skuManageService.getSkuPrice(skuId);
    }

    /**
     * 根据商品SpuID查询海报图片列表
     *
     * @param spuId
     * @return
     */
    @GetMapping("/inner/findSpuPosterBySpuId/{spuId}")
    public List<SpuPoster> getSpuPosterBySpuId(@PathVariable("spuId") Long spuId) {
        return spuManageService.getSpuPosterBySpuId(spuId);
    }


    /**
     * 根据skuId查询当前SKU商品包含平台属性列表(包含属性值)
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getAttrList/{skuId}")
    public List<BaseAttrInfo> getAttrList(@PathVariable("skuId") Long skuId) {
        return skuManageService.getAttrList(skuId);
    }


    /**
     * 查询SPU商品所有销售属性,查询指定SKU销售属性选中效果
     *
     * @param skuId
     * @param spuId
     * @return
     */
    @GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(@PathVariable("skuId") Long skuId, @PathVariable("spuId") Long spuId) {
        return skuManageService.getSpuSaleAttrListCheckBySku(skuId, spuId);
    }

    /**
     * 在一组SPU商品下 切换不同SKU字符串
     *
     * @param spuId "{'销售属性1|销售属性2':'sku商品ID','销售属性1|销售属性3':'sku商品ID'}"
     * @return
     */
    @GetMapping("/inner/getSkuValueIdsMap/{spuId}")
    public String getSkuValueIdsMap(@PathVariable("spuId") Long spuId) {
        return skuManageService.getSkuValueIdsMap(spuId);
    }


    /**
     * 查询所有1级分类列表,要求将二级分类集合包含到一级分类下, 同样三级分类
     *
     * @return
     */
    @GetMapping("/inner/getBaseCategoryList")
    public List<JSONObject> getBaseCategoryList() {
        return baseCategoryViewService.getBaseCategoryList();
    }

    /**
     * 根据品牌ID 查询品牌信息
     *
     * @param tmId
     * @return
     */
    @GetMapping("/inner/getTrademark/{tmId}")
    public BaseTrademark getBaseTrademarkById(@PathVariable("tmId") Long tmId) {
        return baseTrademarkService.getById(tmId);
    }
}
